import { RequireAdapter } from "@/api/types";

/**
 * Electron IPC 自定义适配器
 * @param channel IPC 通道名称
 * @param payload IPC 负载数据
 * @returns Promise<Result<T>>
 */
export const electronIpcAdapter: RequireAdapter<any> = async (channel, payload) => {
  if (!channel) {
    throw new Error("IPC channel name is required");
  }
  // 使用 invoke 发送异步 IPC 消息（推荐方式，Electron 7+）
  try {
    const r = await window.electron.ipcRenderer.invoke(channel, payload);
    console.debug("channel:", channel, "payload:", payload, "result:", r);
    return {
      code: 200,
      msg: "",
      data: r
    };
  } catch (e) {
    return {
      code: 500,
      msg: e.message || `${e}`,
      data: e
    };
  }
};
